﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.CustomerSVC;
using SMS.Entities;

namespace SMS.Model
{
    public class CustomerModel
    {
        ServiceProxy<CustomerMasterServiceClient> proxy = null;

        public event Action<int> OnSaveCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<Customer>> OnSearchCompleted;
        public event Action<SMSException> OnServiceError;
        public event Action<int> OnSaveCustomerTransactionCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<CustomerTransaction>> OnGetCustomerTransactionCompleted;
        public CustomerModel()
        {
            proxy = new ServiceProxy<CustomerMasterServiceClient>("Service/CustomerMasterService.svc");
            proxy.Client.SaveCustomerCompleted += new EventHandler<SaveCustomerCompletedEventArgs>(Client_SaveCustomerCompleted);
            proxy.Client.SearchCustomerCompleted += new EventHandler<SearchCustomerCompletedEventArgs>(Client_SearchCustomerCompleted);
            proxy.Client.SaveCustomerTransactionCompleted += new EventHandler<SaveCustomerTransactionCompletedEventArgs>(Client_SaveCustomerTransactionCompleted);
            proxy.Client.GetCustomerTransactionCompleted += new EventHandler<GetCustomerTransactionCompletedEventArgs>(Client_GetCustomerTransactionCompleted);
        }

        void Client_GetCustomerTransactionCompleted(object sender, GetCustomerTransactionCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnGetCustomerTransactionCompleted != null)
                    OnGetCustomerTransactionCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SaveCustomerTransactionCompleted(object sender, SaveCustomerTransactionCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveCustomerTransactionCompleted != null)
                    OnSaveCustomerTransactionCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SearchCustomerCompleted(object sender, SearchCustomerCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSearchCompleted != null)
                    OnSearchCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SaveCustomerCompleted(object sender, SaveCustomerCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveCompleted != null)
                    OnSaveCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void SaveCustomer(Customer customer)
        {
            proxy.Client.SaveCustomerAsync(App.LoggedinUserName, customer);
        }

        public void SearchCustomer(string SearchText)
        {
            proxy.Client.SearchCustomerAsync(App.LoggedinUserName, SearchText);
        }

        public void GetCustomerTransactionList(int CustomerID, DateTime fromDate, DateTime toDate)
        {
            proxy.Client.GetCustomerTransactionAsync(App.LoggedinUserName, CustomerID, fromDate, toDate);
        }

        public void SaveCustomerTransaction(CustomerTransaction transaction)
        {
            proxy.Client.SaveCustomerTransactionAsync(App.LoggedinUserName, transaction);
        }
    }
}
